package com.zhf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhf.dao.UserMapper;
import com.zhf.domain.*;
import com.zhf.service.UserService;
import com.zhf.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/12/4 22:16
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo findAllUserByPage(UserVo userVo) {
        PageHelper.startPage(userVo.getCurrentPage(),userVo.getPageSize());
        List<User> user = userMapper.findAllUserByPage(userVo);
        PageInfo<User> pageInfo = new PageInfo<>(user);
        return pageInfo;
    }

    @Override
    public User login(User user) throws Exception {

        User user1 = userMapper.login(user);
        if(user1 != null&& Md5.verify(user.getPassword(),"lagou",user1.getPassword())){
            return user1;
        }else {
            return null;
        }

    }

    @Override
    public List<Role> findUserRelationRoleById(Integer id) {
        List<Role> list = userMapper.findUserRelationRoleById(id);
        return list;
    }

    @Override
    public void userContextRole(UserVo userVo) {
        // 1.根据用户ID清空中间表关联关系
        userMapper.deleteUserContextRole(userVo.getUserId());

        // 2.再从新建立关联关系
        for (Integer roleid : userVo.getRoleIdList()) {

            // 封装数据
            User_Role_relation user_role_relation = new User_Role_relation();
            user_role_relation.setUserId(userVo.getUserId());
            user_role_relation.setRoleId(roleid);

            Date date = new Date();
            user_role_relation.setCreatedTime(date);
            user_role_relation.setUpdatedTime(date);

            user_role_relation.setCreatedBy("system");
            user_role_relation.setUpdatedby("system");

            userMapper.userContextRole(user_role_relation);

        }
    }

    @Override
    public ResponseResult getUserPermissions(Integer userid) {
        //获取当前用户拥有的角色
        List<Role> userRelationRoleById = userMapper.findUserRelationRoleById(userid);
        //获取角色id保存到list集合中
        ArrayList<Integer> list = new ArrayList<>();
        for (Role role : userRelationRoleById) {
            list.add(role.getId());
        }
        //根据角色id查询父菜单
        List<Menu> menuList = userMapper.findParentMenuByRoleId(list);
        //查询父菜单关联的子菜单
        for (Menu menu : menuList) {
            List<Menu> subMenuByPid = userMapper.findSubMenuByPid(menu.getId());
            menu.setSubMenuList(subMenuByPid);
        }
        //获取资源信息
        List<Resource> resourceList = userMapper.findResourceByRoleId(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("menuList",menuList);
        map.put("resourceList",resourceList);
        ResponseResult result = new ResponseResult(true, 200, "获取用户权限信息成功", map);
        return result;
    }
}
